<!DOCTYPE html>
<html lang="en" class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>联盟列表查看</title>
    <header th:replace="header::html"></header>
</head>

<body>
<div class="x-nav">
          <span class="layui-breadcrumb">
            <a><cite>首页</cite></a>
            <a><cite>业务管理</cite></a>
            <a><cite>联盟列表</cite></a>
          </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>

<div class="layui-fluid">
    <div class="layui-row  layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form  layui-col-space5">
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" name="aname" placeholder="请输入联盟名或其关键字" autocomplete="off"
                                   class="layui-input">
                        </div>

                        <div class="layui-input-inline">
                            <select name="atype" lay-filter="atype" id="atypeSelect">
                            </select>
                        </div>

                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" lay-submit="" lay-filter="search"><i
                                    class="layui-icon">&#xe615;</i></button>
                        </div>
                    </form>
                </div>
                <div class="layui-card-body ">
                    <table class="layui-hide" id="backAlliance" lay-filter="backAlliance"></table>
                </div>

                <div class="layui-card-body ">
                    <script type="text/html" id="barDemo">
                        <a class="layui-btn layui-btn-xs" lay-event="detail"
                        >查看详情</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"
                        >解散联盟</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/html" id="msg2">
    <table id="table2" lay-filter="test3"></table>
</script>
<script type="text/html" id="bar2">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="run">踢出联盟</a>
</script>
<script>
	$(function () {
		$.ajax({
			url: "/user/service",
			data: {},
			dataType: "json",
			success: function (results) {
				$("#atypeSelect").empty();
				if (results !== 0) {
					$("#atypeSelect").append(new Option("---请选择联盟类型---", ""));
					$.each(results, function (index, item) {
						$('#atypeSelect').append(new Option(item.sname, item.sid));
					});
				} else {
					$("#atypeSelect").append(new Option("暂无数据", ""));
				}
				layui.form.render("select");
			}
		})
	});

	layui.use('table', function () {
		var table = layui.table,
			form = layui.form;
		table.render({
			elem: '#backAlliance'
			, url: '/api-back/backAlliance/list' //数据接口
			, page: true //开启分页
			, cellMinWidth: 80 //全局定义常规单元格的最小宽度
			, response: {
				dataName: 'datas' //规定数据列表的字段名称，默认：data
			}
			, cols: [
				[ //表头
					{type: 'checkbox', fixed: 'left'},
					{title: '序号', type: 'numbers', width: '5%'}
					, {field: 'aid', title: 'ID', align: 'center', sort: true, width: "5%", style: 'display:none;'}
					, {field: 'aname', title: '联盟名称', align: 'center'}
					, {field: 'ainfo', title: '联盟介绍', align: 'center', sort: true, width: "5%"}
					, {field: 'tips1', title: '联盟类型', align: 'center'}
					, {field: 'amargin', title: '入盟费用', align: 'center'}
					, {field: 'abank', title: '联盟账户', align: 'center'}
					, {field: 'amasterid', title: '创建者账号', align: 'center', width: "5%"}
					, {field: 'aaccout', title: '联盟账号', align: 'center', width: "5%"}
					, {title: '操作', toolbar: '#barDemo', align: 'center'}
				]
			],
			done: function () {
				//checkPermission();
				$('table.layui-table thead tr th:eq(2)').addClass('layui-hide');//隐藏第一列的表头
			}
		});


		//监听工具条
		table.on('tool(backAlliance)', function (obj) {
			var data = obj.data;//获取点击行数据
			if (obj.event === 'del') {
				layer.confirm('确认解散该联盟吗？（WARMING：请注意，解散后不可逆转，请确认后再解散）', function (index) {
					$.ajax({
						url: "/api-back/backAlliance/deleteAlliance",
						type: "GET",
						data: {
							aid: data.aid,
							amargin: data.amargin,
							amasterid: data.amasterid
						},
						dataType: 'json',
						success: function (result) {
							layer.alert("解散成功！", {icon: 1}, function (index1) {
								layer.close(index1);
								table.reload('backAlliance');
								$('table.layui-table thead tr th:eq(2)').addClass('layui-hide');//隐藏第一列的表头
							});
						},
						error: function () {
							//window.location.href = "/404.html";
							layer.open({
								type: 2,
								content: "/404.html",
								area: ['60%', '60%'],
								title: '权限不足'
							});
						}
					});
				});
			} else if (obj.event === 'detail') {
				// xadmin.open('联盟成员详情', '/backAlliance/detail/?aid=' + data.aid);
				var table2 = layui.table;
				var str = layer.open({
					type: 1,
					content: $('#msg2').html(),
					area: ['800px', '500px'],
					title: '我的联盟',
					btn: ['返回'],
					success: function () {
						table2.render({
							elem: $('#table2')
							, type: 'POST'
							, url: '/api-back/backAlliance/getUsers'
							, page: true
							, where: {aid: data.aid, amasterid: data.amasterid}
							, limit: 5
							, limits: [5]
							, cols: [
								[
									{field: 'uid', hide: true}
									, {field: 'uname', title: '用户名', width: 200}
									, {field: 'uscore', title: '信用分', width: 200}
									, {field: 'tips1', title: '服务类型', width: 200}
									, {title: '操作', width: 150, toolbar: "#bar2"}
								]
							]
						});

						table.on('tool(test3)', function (obj) {
							var d = obj.data;
							layer.confirm('确定要将' + d.uname + '踢出联盟吗?', function (index3) {
								$.ajax({
									type: "POST",
									url: '/api-front/alliance/outAlliance',
									dataType: "text",
									data: {aid: data.aid, money: data.amargin, uid: d.uid},
									success: function (msg) {
										if (msg === "yes") {
											layer.alert("操作成功", {icon: 1});
											layer.close(str);
										} else {
											layer.alert("操作失败，请重试", {icon: 5});
										}
									},
									error: function () {
										layer.alert("服务器正忙.....", {icon: 5});
									}
								});
							});
						});
					},
					error: function () {
						//window.location.href = "/404.html";
						layer.open({
							type: 2,
							content: "/404.html",
							area: ['60%', '60%'],
							title: '权限不足'
						});
					}
				});
			}
		});

		//搜索
		form.on('submit(search)', function (data) {
			var data = data.field;
			table.render({
				elem: '#backAlliance'
				, url: '/api-back/backAlliance/findAllianceByFuzzy' //数据接口
				, page: true //开启分页
				, where: {
					"aname": data.aname,
					"sid": data.atype
				}
				, response: {
					countName: 'count', //规定数据总数的字段名称，默认：count
					dataName: 'datas' //规定数据列表的字段名称，默认：data
				}
				, cols: [
					[ //表头
						{type: 'checkbox', fixed: 'left'},
						{title: '序号', type: 'numbers', width: '5%'}
						, {field: 'aid', title: 'ID', align: 'center', sort: true, width: "5%", style: 'display:none;'}
						, {field: 'aname', title: '联盟名称', align: 'center'}
						, {field: 'ainfo', title: '联盟介绍', align: 'center', sort: true, width: "5%"}
						, {field: 'tips1', title: '联盟类型', align: 'center'}
						, {field: 'amargin', title: '入盟费用', align: 'center'}
						, {field: 'abank', title: '联盟账户', align: 'center'}
						, {field: 'amasterid', title: '创建者账号', align: 'center', width: "5%"}
						, {field: 'aaccout', title: '联盟账号', align: 'center', width: "5%"}
						, {title: '操作', toolbar: '#barDemo', align: 'center'}
					]
				],
				done: function () {
					//checkPermission();
					$('table.layui-table thead tr th:eq(2)').addClass('layui-hide');//隐藏第一列的表头
				},
				error:function () {
					layer.open({
						type: 2,
						content: "/404.html",
						area: ['60%', '60%'],
						title: '权限不足'
					});
				}
			});
			return false;
		});
		//checkPermission();
	});
</script>
</html>